{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cb323ff-c445-4b4d-a86f-899ec6a993e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ollama"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d90d29f4-651e-4228-9cbf-10a08b5aadf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def send_messages(messages, tools):\n",
    "    response = ollama.chat(\n",
    "        model=\"llama3.2:latest\",\n",
    "        messages=messages,\n",
    "        tools=tools\n",
    "    )\n",
    "    return response.message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "565aec1d-c35d-4861-9123-f136ecc49c79",
   "metadata": {},
   "outputs": [],
   "source": [
    "tools = [\n",
    "    {\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"get_weather\",\n",
    "            \"description\": \"Get weather of an location, the user shoud supply a location first\",\n",
    "            \"parameters\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"location\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"location\"]\n",
    "            },\n",
    "        }\n",
    "    },\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b6a8c1ca-165d-4257-bcbb-a264f678065e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "role='assistant' content='' thinking=None images=None tool_calls=[ToolCall(function=Function(name='get_weather', arguments={'location': 'Shanghai, China'}))]\n",
      "User>\t How's the weather in Shanghai?\n"
     ]
    }
   ],
   "source": [
    "messages = [{\"role\":\"user\", \"content\":\"How's the weather in Shanghai?\"}]\n",
    "message = send_messages(messages, tools)\n",
    "\n",
    "#print(f\"User>\\t {messages['message']['content']}\")\n",
    "print(message)\n",
    "print(f\"User>\\t {messages[0]['content']}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4b809848-2d1c-41ea-b6a2-539dff7d80c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "tool = message.tool_calls[0]\n",
    "messages.append(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "028b8b66-1ef2-47e5-abf4-5e618d0ccac7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ToolCall(function=Function(name='get_weather', arguments={'location': 'Shanghai'}))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e581bd15-ad55-4f55-b598-b455c6f2b4f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model>\t The current temperature in Shanghai is 24 degrees Celsius (°C).\n"
     ]
    }
   ],
   "source": [
    "# 模拟 search_web 工具调用结果（直接返回24度）\n",
    "messages.append({\"role\": \"tool\",\"name\": \"get_weather\",\"content\": \"24℃\"})\n",
    "#messages.append({\"role\": \"tool\", \"tool_call_id\": tool.id, \"content\": \"24℃\"})\n",
    "message = send_messages(messages, tools)\n",
    "print(f\"Model>\\t {message.content}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3a1aecf-96ac-4122-88ca-a6e1670c8746",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
